/*
 * ====================================================================
 *
 * Copyright (c) 2002 Caravel Technologies. All rights reserved.
 *
 * For information about the company, visit <http://www.caraveltech.com>.
 *
 * ====================================================================
 */
package ex1;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

/**
 * Implementation of <strong>Action</strong> that accepts input for
 * the user profile.
 *
 * @author Wellie W. Chao
 * @version $Revision: 1.1 $ $Date: 2002/05/26 16:25:26 $
 */
public final class ProfileAction extends Action {

    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if business logic throws an exception
     */
    @Override
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm baseForm,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws IOException, ServletException {

        ProfileForm profileForm = null;
        String cible = "success";
        if (baseForm != null) {
            profileForm = (ProfileForm) baseForm;
            request.setAttribute("firstName", profileForm.getFirstName());
            request.setAttribute("lastName", profileForm.getLastName());
            request.setAttribute("favoriteColor", profileForm.getFavoriteColor());
            request.setAttribute("birthdate", profileForm.getBirthdate());
            request.setAttribute("adress", profileForm.getAdress());
            request.setAttribute("phone", profileForm.getPhone());
        }
        else {
            cible = "echec";
            ActionMessages erreurs = new ActionMessages();
            erreurs.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("error.unknown", null));

            // Signalement des erreurs a la page d'origine
            if (!erreurs.isEmpty()) {
                saveErrors(request, erreurs);
            }
        }

        return (mapping.findForward(
                cible));
    }
}
